package com.hyzh.latte.vehicle.dmo;

import com.hyzh.latte.vehicle.nezha.PoleVertex;
import lombok.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author songkui
 * @since 2024/3/21 10:38
 */
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(of = {"objectId"})
@ToString
public class VehicleBO {

    private String parkCode; //园区编号

    private String objectId; //汽车编码
    private String vehicleNo; // 汽车号牌
    private String type; // 车辆类型
    private int   v;    //速度；


    //    private PoleVertex lastPole; // 上一个杆子
    private PoleVertex  currentPole; // 现在处于杆子
    private BigDecimal time = new BigDecimal("0");
    private long second; //    long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));

    private long newSecond;


    public VehicleBO(String parkCode, String objectId, String vehicleNo, String type) {
        this.parkCode = parkCode;
        this.objectId = objectId;
        this.vehicleNo = vehicleNo;
        this.type = type;
    }

    public void newSecond(){
        this.newSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    }

    public double buildTime(double t) {
        if (second == 0) {
            return 0;
        }
        second = 1;
        time = time.add(new BigDecimal(String.valueOf(t)).multiply(new BigDecimal("2.5")));
        return time.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public double buildTimeTrack(double t, int v) {
        if (second == 0) {
            second = newSecond;
            return 0;
        }
        if (newSecond != second) {
            long diff = newSecond - second;
            long st = time.longValue();
            second = newSecond;

            if (diff < st) {
                time = new BigDecimal(String.valueOf(st-diff));
                return time.doubleValue();
            } else {
                time = new BigDecimal("0");
                return 0;
            }
        }
//        time = time.add(new BigDecimal(String.valueOf(t)).multiply(new BigDecimal("9.8")));
        time = time.add(computeTime(new BigDecimal(String.valueOf(t)), v));
        return time.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    private static BigDecimal computeTime(BigDecimal t, int v){
        int vv = v < 10 ? 60 : Math.min(v, 120);
        return t.multiply(new BigDecimal(String.valueOf(3.2 + (double) 100/vv)));
    }


}
